//
//  ZHScreenCaptureVC.m
//  test02
//
//  Created by lzh on 2020/10/15.
//  Copyright © 2020 NO. All rights reserved.
//

#import "ZHScreenCaptureVC.h"

@interface ZHScreenCaptureVC ()

@property(nonatomic,strong)UIImageView * imV;

@property(nonatomic,strong)UIImageView * backView;

@property(nonatomic,strong)UIButton * btn;
@end

@implementation ZHScreenCaptureVC

//-(void)viewWillAppear:(BOOL)animated{
//    
//    [super viewWillAppear:animated];
//    
//    [self.navigationController.navigationBar setHidden:YES];
//}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    
    self.view.backgroundColor = [UIColor whiteColor];
    
      self.backView = [[UIImageView alloc]initWithFrame:self.view.bounds];
        
        self.backView.hidden =YES;
        [self.view addSubview:self.backView];
        
        UIImageView * imageV = [[UIImageView alloc]initWithFrame:CGRectMake(50, 100, self.view.bounds.size.width-100, self.view.bounds.size.height-200)];
        
        imageV.image = [UIImage imageNamed:@"1"];
        
        [self.view addSubview:imageV];
        
        self.imV = imageV;
        
        UIButton * butn = [UIButton buttonWithType:UIButtonTypeCustom];
        
        [butn setTitle:@"123" forState:UIControlStateNormal];
        
        butn.backgroundColor = [UIColor redColor];
        [butn addTarget:self action:@selector(btnclick) forControlEvents:UIControlEventTouchUpInside];
        
        butn.frame = CGRectMake(100, self.view.bounds.size.height-80, 100, 50);
        [self.view addSubview:butn];
        
        self.btn = butn;
}

-(void)btnclick{
        
        UIGraphicsBeginImageContextWithOptions(self.imV.bounds.size, 1, 0.0);
    #pragma mark self.imV.frame 是基于画布的。也就是上面self.view.bounds.size的 会在这个size里 按照frame 来绘制
        [self.imV drawViewHierarchyInRect:self.imV.bounds afterScreenUpdates:NO];
        UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        
        self.backView.image = image;
        
        self.backView.hidden= NO;
        
        self.btn.hidden = YES;
        
        self.imV.hidden = YES;

}


@end
